package com.wkq.main.ui

import android.Manifest
import android.content.Context
import android.content.Intent
import android.location.Location
import com.wkq.base.activity.BasicVbActivity
import com.wkq.base.util.showToast
import com.wkq.main.databinding.ActivityLocationBinding
import com.wkq.util.NativeLocationHelper
import com.wkq.util.NativeLocationSingleHelper
import com.wkq.util.setSafeClickListener

/**
 *
 *@Author: wkq
 *
 *@Time: 2025/8/25 9:22
 *
 *@Desc:
 */
class LocationActivity : BasicVbActivity<ActivityLocationBinding>() {

    companion object {
        fun startActivity(context: Context) {
            context.startActivity(Intent(context, LocationActivity::class.java))
        }
    }

    val permissions = mutableListOf<String>(
        Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION
    )
    var locationHelper: NativeLocationHelper? = null
    var locationSingleHelper: NativeLocationSingleHelper? = null
    private val callback: (location: Location?) -> Unit = { location ->

        binding.tvLocation.text =
            "提供者: ${location?.provider}" + "纬度: ${location?.latitude}, 经度: ${location?.longitude}"
    }



    override fun initView() {
        hasRequest = isGranted(permissions)

        // 单次定位
        binding.btInitSingle.setSafeClickListener {
            locationSingleHelper = NativeLocationSingleHelper(
                this, timeout = 5000L, callback = callback
            )
        }

        binding.btSingleLocation.setSafeClickListener {
            showSingleInit()
            locationSingleHelper?.startLocation()
        }

        binding.btSingleGps.setSafeClickListener {
            if (!hasRequest) {
                requestLocation()
                return@setSafeClickListener
            }
            showSingleInit()
            locationSingleHelper?.getSingleGpsLocation()
        }

        binding.btSingleNet.setSafeClickListener {
            if (!hasRequest) {
                requestLocation()
                return@setSafeClickListener
            }
            showSingleInit()
            locationSingleHelper?.getSingleNetLocation()
        }
        binding.btSingleCache.setSafeClickListener {
            if (!hasRequest) {
                requestLocation()
                return@setSafeClickListener
            }
            showSingleInit()
            locationSingleHelper?.getCacheLocation()
        }


        // 多次定位

        binding.btInit.setSafeClickListener {
            locationHelper = NativeLocationHelper(
                this, timeout = 5000L, minTimeMs = 3000L, minDistanceM = 1f, callback = callback
            )
        }


        binding.btLocation.setSafeClickListener {
            if (!hasRequest) {
                requestLocation()
                return@setSafeClickListener
            }
            showInit()
            locationHelper?.startLocation()
        }

        binding.btGps.setSafeClickListener {
            if (!hasRequest) {
                requestLocation()
                return@setSafeClickListener
            }
            showInit()
            locationHelper?.getGpsLocation()
        }

        binding.btNet.setSafeClickListener {
            if (!hasRequest) {
                requestLocation()
                return@setSafeClickListener
            }
            showInit()
            locationHelper?.getNetLocation()
        }



        binding.btCache.setSafeClickListener {
            if (!hasRequest) {
                requestLocation()
                return@setSafeClickListener
            }
            showInit()
            locationHelper?.getCacheLocation()

        }


        binding.btReset.setSafeClickListener {
            binding.tvLocation.text = "未知位置"
        }

        binding.btRelease.setSafeClickListener {
            binding.tvLocation.text = "未知位置"
            locationHelper?.release()
            locationSingleHelper?.release()
        }


    }


    fun requestLocation() {
        requestPermissionsLauncher(10010, permissions)
    }

    override fun initData() {

    }

    fun showInit() {
        if (locationHelper == null) {
            showToast("请先初始化")
        }
    }

    fun showSingleInit() {
        if (locationSingleHelper == null) {
            showToast("请先初始化")
        }
    }

    var hasRequest = false

    override fun authorized(permissionType: Int, permissionList: MutableList<String>) {
        if (permissionType == 10010) {
            hasRequest = true
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        locationHelper?.release()
        locationSingleHelper?.release()
    }


}